package com.example.smartall.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.diaglog.LoadedDialog;
import com.example.smartall.entity.HospitalCardBean;
import com.example.smartall.entity.UserInfoBean;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;
import com.example.smartall.utils.TextUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * 创建病人信息卡
 */
public class CreatePatientCardActivity extends BaseActivity {

    private HospitalCardBean.RowsBean cardBean;
    //两种模式 一种是修改卡片信息 一种是新增卡片 若进入此活动未获取到传递的病人信息 则为创建 否则为更改
    private boolean isChange = false;

    private ImageView imBack;
    private TextView tvTitle;
    private EditText etName;
    private EditText etSex;
    private EditText etIdCard;
    private EditText etBirth;
    private EditText etAddress, etPhone;
    private Button btOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_patient_card);
        //假try
        try {
            cardBean = (HospitalCardBean.RowsBean) getIntent().getSerializableExtra("cardInfo");
            isChange = getIntent().getBooleanExtra("isChange", false);
        } catch (Exception e) {
            Log.i("TAG", "Create Card Data Is Null");
        }
        init();
        initEvent();
        if (cardBean != null) {
            initData();
        }
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        etName = (EditText) findViewById(R.id.et_name);
        etSex = (EditText) findViewById(R.id.et_sex);
        etIdCard = (EditText) findViewById(R.id.et_idCard);
        etBirth = (EditText) findViewById(R.id.et_birth);
        etAddress = (EditText) findViewById(R.id.et_address);
        etPhone = (EditText) findViewById(R.id.et_phone);
        btOk = (Button) findViewById(R.id.bt_ok);

        if (isChange) {
            tvTitle.setText("修改就诊卡");
        } else {
            tvTitle.setText("新建就诊卡");
        }
    }

    private void initData() {
        etName.setText(cardBean.getName());
        etSex.setText(cardBean.getSex().equals("0") ? "男" : "女");
        etPhone.setText(cardBean.getTel());
        etBirth.setText(cardBean.getBirthday() + "");
        etIdCard.setText(cardBean.getCardId());
        etAddress.setText(cardBean.getAddress());
    }

    @Override
    public void initEvent() {
        btOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.hasEmpty(etName, etAddress, etIdCard, etBirth, etSex, etPhone)) {
                    Toast.makeText(CreatePatientCardActivity.this, "有未填项目", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isChange) {
                    changeCard();
                } else {
                    crateCard();
                }
            }
        });
    }

    //修改卡片
    private void changeCard() {
        HashMap<String, Object> params = getData();
        params.put("id", cardBean.getId());
        JSONObject jsonObject = new JSONObject(params);
        String json = jsonObject.toString();
        Api.config(ApiConfig.HOSPITAL_CHANGE_CARD, null).putRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                showToastSync("修改成功");
                CreatePatientCardActivity.this.finish();
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("修改失败");
            }
        }, CreatePatientCardActivity.this, json);
    }

    //创建卡片
    private void crateCard() {
        final LoadedDialog dialog = new LoadedDialog(CreatePatientCardActivity.this);
        dialog.show();
        HashMap<String, Object> params = getData();
        Api.config(ApiConfig.HOSPITAL_ADD_PATIENT, params).postRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                if (res.contains("200")) {
                    dialog.dismiss();
                    showToastSync("创建成功");
                    destroyThis();
                }
            }

            @Override
            public void onFailure(Exception e) {
                showToastSync("创建失败");
            }
        }, SPUtils.config(CreatePatientCardActivity.this, Constant.SP_USER_FILE_NAME).getString(Constant.TOKEN));
    }

    private HashMap<String, Object> getData() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("name", etName.getText().toString().trim());
        params.put("sex", etSex.getText().toString().trim().equals("男") ? "0" : "1");
        params.put("cardId", etIdCard.getText().toString().trim());
        params.put("tel", etPhone.getText().toString().trim());
        params.put("birthday", etBirth.getText().toString().trim());
        params.put("address", etAddress.getText().toString().trim());
        return params;
    }
}